// ['0-0', '0-1-1', '0-2', '0-2-2'] ===>  ['0-0', '0-1', '0-1-1', '0-2', '0-2-2']

// author gp25 宽哥
export const formatArr = (arr) => {
  const result = arr.reduce((_arr, item) => [..._arr, ...sliceStr(item)], [])
  return [...new Set(result)].sort() // 左侧菜单栏顺序
}
const sliceStr = (str) => {
  if (str.split('-').length > 2) {
    return str.split('-').reduce((_arr, item) => {
      if (!Array.isArray(_arr)) return [`${_arr}-${item}`]
      return [`${_arr[0]}-${item}`, ..._arr]
    })
  }
  return [str]
}
// 截取字符串  数组合并   数组去重
export function getCheckedKeys (checkedKeys) {
  const arr = []
  checkedKeys.forEach(item => {
    arr.push(item.slice(0, 3))
  })
  const result = Array.from(new Set([...checkedKeys, ...arr])).sort()
  return result
}

// 获取左侧的菜单栏数据
export function getMenu (menus,checkedKeys) {
  let arr = []
  checkedKeys.forEach(item => {
    menus.forEach(value => {
      if(value.key === item) {
        // arr.push({...value})
        arr.push(Object.assign({}, value))
      }
    })
  })
  // 搞定第二层级
  arr.forEach(item => {
    if(item.children) {
      let newArr = getMenu(item.children,checkedKeys)
      item.children = newArr
    }
  })
  return arr
}

// 判断pathname是不是在menus中
// export function checkPathName (menus: IRoute[], pathname: string) {
//   // console.log(menus)
//   const flag =  menus.some(item => {
//     if (item.children) {
//       return checkPathName(item.children, pathname)
//     } else {
//       return item.path === pathname
//     }
//   })
//   // console.log(flag)
//   return flag
// }

export function checkPathName (routes, pathname, arr) {
  // const arr: string[] = []
  routes.forEach(item => {
    if (item.children) {
      checkPathName(item.children, pathname, arr)
    }
    arr.push(item.path)
  })
  console.log('9999', arr)
  return arr.indexOf(pathname) !== -1
}